package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.domain.Recordbug;
import com.woniuxy.dto.*;
import com.woniuxy.mapper.RecordbugMapper;
import com.woniuxy.parameter.PageParam;
import com.woniuxy.parameter.QueryBugParam;
import com.woniuxy.service.RecordbugService;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author XXX
 * @since 2021-05-12
 */
@Service
public class RecordbugServiceImpl extends ServiceImpl<RecordbugMapper, Recordbug> implements RecordbugService {

    @Resource
    private RecordbugMapper recordbugMapper;

    @Override
    public List<Recordbug> queryRecordbug(String classify, Date date1, Date date2) {
        List<Recordbug> recordbugs = recordbugMapper.queryRecordbug(classify, date1, date2);
        return recordbugs;
    }

    @Override
    public List<Recordbug> queryRecordbugState(String dostate, Date date1, Date date2) {
        List<Recordbug> recordbugList = recordbugMapper.queryRecordbugState(dostate, date1, date2);
        return recordbugList;
    }

    @Override
    public List<Recordbug> queryRecordbugDegree(String degree, Date date1, Date date2) {
        List<Recordbug> recordbugList = recordbugMapper.queryRecordbugDegree(degree, date1, date2);
        return recordbugList;
    }

    /*===================================================================================*/

    @Override
    public DtoMyPage<DtoSubmitBugInfo> searchForAllBug(int current, int size, int userId) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Page<Recordbug> pages = new Page<>(current, size);
        QueryWrapper<Recordbug> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        IPage<Recordbug> iPage = recordbugMapper.selectPage(pages, wrapper);
        ArrayList<DtoSubmitBugInfo> arrayList = new ArrayList<>();
        for (Recordbug recordbug : iPage.getRecords()) {
            DtoSubmitBugInfo dtoSubmitBugInfo = new DtoSubmitBugInfo();
            PropertyUtils.copyProperties(dtoSubmitBugInfo, recordbug);
            arrayList.add(dtoSubmitBugInfo);
        }
        DtoMyPage<DtoSubmitBugInfo> dtoMyPage = new DtoMyPage<>();
        dtoMyPage.setRecords(arrayList);
        dtoMyPage.setTotal(iPage.getTotal());

        return dtoMyPage;
    }

    @Override
    public ArrayList<DtoSubmitBugInfo> searchByInfo(String projectId, String state, String info, int userId) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        List<Recordbug> list = recordbugMapper.getSearchInfo(projectId, state, info, userId);
        ArrayList<DtoSubmitBugInfo> arrayList = new ArrayList<>();
        for (Recordbug recordbug : list) {
            DtoSubmitBugInfo dtoSubmitBugInfo = new DtoSubmitBugInfo();
            PropertyUtils.copyProperties(dtoSubmitBugInfo, recordbug);
            arrayList.add(dtoSubmitBugInfo);
        }
        return arrayList;
    }

    @Override
    public ArrayList<Integer> searchForAllProject(int userId) {
        QueryWrapper<Recordbug> wrapper = new QueryWrapper<>();
        wrapper.select("distinct project_id").eq("user_id", userId);
        List<Recordbug> list = recordbugMapper.selectList(wrapper);
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (Recordbug recordbug : list){
            arrayList.add(recordbug.getProjectId());
        }
        return arrayList;
    }

    @Override
    public boolean updateState(DtoSubmitBugInfoVO dtoSubmitBugInfoVO) {
        QueryWrapper<Recordbug> wrapper = new QueryWrapper<>();
        wrapper.eq("bug_id", dtoSubmitBugInfoVO.getInfo().getBugId());
        Recordbug recordbug = recordbugMapper.selectOne(wrapper);
        recordbug.setDostate("已完成");
        int i = recordbugMapper.updateById(recordbug);
        if (i != 0) {
            return true;
        } else {
            return false;
        }
    }


    @Override
    public PageInfo queryBugByNameOrState(QueryBugParam queryBugParam, PageParam pages) {
        List<Recordbug> recordbugList = null;
        PageHelper.startPage(pages.getPageNum(), pages.getPageSize());
        if ("" == queryBugParam.getProjectName()) {
            recordbugList = recordbugMapper.queryBugByState(queryBugParam.getState());
        } else {
            recordbugList = recordbugMapper.queryBugByNameAndState(queryBugParam.getState(), queryBugParam.getProjectName());
        }
        PageInfo pageInfo = new PageInfo(recordbugList);
        return pageInfo;
    }
    @Override
    public ArrayList<Integer> getAllProjectClass() {
        QueryWrapper<Recordbug> wrapper = new QueryWrapper<>();
        wrapper.select("distinct project_id");
        List<Recordbug> list = recordbugMapper.selectList(wrapper);
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (Recordbug recordbug : list) {
            arrayList.add(recordbug.getProjectId());
        }
        return arrayList;
    }

    @Override
    public DtoProjectInfoOfClassify countProjectClassify(int projectId) {
        QueryWrapper<Recordbug> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("bug_name", "数据错误").eq("project_id", projectId);
        int dataError = recordbugMapper.selectCount(wrapper1);

        QueryWrapper<Recordbug> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("bug_name", "参数校验").eq("project_id", projectId);
        int checkParam = recordbugMapper.selectCount(wrapper2);

        QueryWrapper<Recordbug> wrapper3 = new QueryWrapper<>();
        wrapper3.eq("bug_name", "界面排版").eq("project_id", projectId);
        int interfaceLayout = recordbugMapper.selectCount(wrapper3);

        QueryWrapper<Recordbug> wrapper4 = new QueryWrapper<>();
        wrapper4.eq("bug_name", "未满足需求").eq("project_id", projectId);
        int notEnoughDemand = recordbugMapper.selectCount(wrapper4);

        DtoProjectInfoOfClassify dtoProjectInfoOfClassify = new DtoProjectInfoOfClassify();
        dtoProjectInfoOfClassify.setDataError(dataError);
        dtoProjectInfoOfClassify.setCheckParam(checkParam);
        dtoProjectInfoOfClassify.setInterfaceLayout(interfaceLayout);
        dtoProjectInfoOfClassify.setNotEnoughDemand(notEnoughDemand);

        return dtoProjectInfoOfClassify;
    }

    @Override
    public DtoProjectInfoOfState countProjectState(int projectId) {
        QueryWrapper<Recordbug> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("dostate", "待分配").eq("project_id", projectId);
        int toBeAllocated = recordbugMapper.selectCount(wrapper1);

        QueryWrapper<Recordbug> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("dostate", "待处理").eq("project_id", projectId);
        int toBeResolved = recordbugMapper.selectCount(wrapper2);

        QueryWrapper<Recordbug> wrapper3 = new QueryWrapper<>();
        wrapper3.eq("dostate", "处理中").eq("project_id", projectId);
        int underWay = recordbugMapper.selectCount(wrapper3);

        QueryWrapper<Recordbug> wrapper4 = new QueryWrapper<>();
        wrapper4.eq("dostate", "已完成").eq("project_id", projectId);
        int completed = recordbugMapper.selectCount(wrapper4);

        DtoProjectInfoOfState dtoProjectInfoOfState = new DtoProjectInfoOfState();
        dtoProjectInfoOfState.setToBeAllocated(toBeAllocated);
        dtoProjectInfoOfState.setToBeResolved(toBeResolved);
        dtoProjectInfoOfState.setUnderWay(underWay);
        dtoProjectInfoOfState.setCompleted(completed);

        return dtoProjectInfoOfState;
    }

    @Override
    public DtoProjectInfoOfDegree countProjectDegree(int projectId) {
        QueryWrapper<Recordbug> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("degree", "轻度").eq("project_id", projectId);
        int light = recordbugMapper.selectCount(wrapper1);

        QueryWrapper<Recordbug> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("degree", "普通").eq("project_id", projectId);
        int general = recordbugMapper.selectCount(wrapper2);

        QueryWrapper<Recordbug> wrapper3 = new QueryWrapper<>();
        wrapper3.eq("degree", "严重").eq("project_id", projectId);
        int serious = recordbugMapper.selectCount(wrapper3);

        DtoProjectInfoOfDegree dtoProjectInfoOfDegree = new DtoProjectInfoOfDegree();
        dtoProjectInfoOfDegree.setLight(light);
        dtoProjectInfoOfDegree.setGeneral(general);
        dtoProjectInfoOfDegree.setSerious(serious);

        return dtoProjectInfoOfDegree;

    }
}
